<?php
use app\models\Poster;
use common\component\tool\Pages;
use common\component\tool\Tool;

/*导航信息*/
function smarty_block_poster($params, $content, Smarty_Internal_Template $template, &$repeat)
{
	// $repeat = false;
    $app_id = Yii::$app->params['app_id'];
    if(isset($_GET['page'])){
        $currPage = intval($_GET['page']?$_GET['page']:1);
    }else{
        $currPage = 1;
    }
    // $pageSize = Tool::getPageSize();
    // P($repeat, false);
    $pageSize = 10;

    $poster = new Poster;
    $count = $poster::find()->where('app_id = :app_id', [':app_id' => $app_id])->count();
    $now = date('Y-m-d H:i:s', time());
    $posterList = $poster::find()
                    ->select(['id', 'name', 'location', 'preview', 'url_target'])
                    ->where("app_id = :app_id and (start_time < '$now' and end_time > '$now') and display = 2", [':app_id' => $app_id])
                    ->orderBy(['id' => SORT_DESC])
                    ->offset($pageSize*($currPage-1))->limit($pageSize)
                    ->asArray()->all();
    $isMobile = Tool::isMobile();//判断是否手机访问
    // echo count($posterList);
    foreach($posterList as $k => $v){
        $posterList[$k]['preview'] = explode(',', $posterList[$k]['preview']);
        $posterList[$k]['url'] = '/poster/' . $posterList[$k]['id'];
        $posterList[$k]['target'] = ($posterList[$k]['url_target'] == 1)?'_self':'_blank';
        unset($posterList[$k]['url_target']);
	    if($repeat){
            if($isMobile){//判断是否手机访问
                $poster->updateAllCounters(['mobile_show_count' => 1], 'id = :id', [':id' => $v['id']]);
            }else{
                // echo $v['id'];
    	    	$poster->updateAllCounters(['pc_show_count' => 1], 'id = :id', [':id' => $v['id']]);
            }
    	}
    }
    // P($posterList);
    $pages = new Pages($count);


    $pageInfo = array(
        'count' => intval($count),
        'currPage' => $currPage,
        'pageSize' => $pageSize,
        'html' => $pages->fpage(isset($params['pageArgs'])?$params['pageArgs']:[0,1,2,3,4,5,6,7,8]),
    );
    
    $template->assign("list", $posterList);
    $template->assign("pageInfo", $pageInfo);
    return $content;

}